﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace nbb.darbo_grafikas
{
    internal partial class FormSelectDefectoscopes : Form
    {
        internal FormSelectDefectoscopes()
        {
            InitializeComponent();
        }

        private void FormSelectDefectoscopes_Load(object sender, EventArgs e)
        {
            dgv.AutoGenerateColumns = false;            
            foreach (DataGridViewColumn col in dgv.Columns)
            {
                col.DataPropertyName = col.Name;               
            }
            dgv.DataSource = Program.Defektoskopai;

            foreach (DataGridViewRow dr in dgv.Rows)
            {
                ((DataGridViewCheckBoxCell)dr.Cells["Itraukti"]).Value = true;
            }
            lblCount.Text = string.Format("Pažymėta: {0} defektoskop.", Program.Defektoskopai.Count);
        }

        private void btnForward_Click(object sender, EventArgs e)
        {
            List<int> idsToRemove = new List<int>();
            foreach (DataGridViewRow dr in dgv.Rows)
            {
                if (!(bool)dr.Cells["Itraukti"].Value)
                {
                    idsToRemove.Add((int)dr.Cells["Id"].Value);
                }                
            }
            
            Program.Defektoskopai.RemoveAll(defektoskopas => idsToRemove.Contains(defektoskopas.Id));
            

            // do something to show next form
        }

        private void btnRenewList_Click(object sender, EventArgs e)
        {
            // function to reload defektoskopai from database
            dgv.DataSource = Program.Defektoskopai;

            foreach (DataGridViewRow dr in dgv.Rows)
            {
                ((DataGridViewCheckBoxCell)dr.Cells["Itraukti"]).Value = true;
            }
        }        

        private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv.Columns[e.ColumnIndex].Name == "Itraukti")
                dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            lblCount.Text = string.Format("Pažymėta: {0} defektoskop.", countSelected());
        }

        private int countSelected()
        {
            int selected = 0;
            foreach (DataGridViewRow dr in dgv.Rows)
            {
                ((DataGridViewCheckBoxCell)dr.Cells["Itrauktas"]).Value = true;
                selected++;
            }
            return selected;
        }
    }
}
